package cn.rjzjh.tapestry.tams.pages.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.tapestry5.util.TextStreamResponse;

import cn.rjzjh.commons.util.apiext.JSONUtil;
import cn.rjzjh.commons.util.apiext.StringUtil;
import cn.rjzjh.tapestry.tams.base.ParentPage;

public class ComponentTest extends ParentPage {

	public static final List<Map<String, String>> jsonList = new ArrayList<Map<String, String>>();
	public static final List<Map<String, String>> jsonList2 = new ArrayList<Map<String, String>>();

	static {
		Map<String, String> obj1 = new HashMap<String, String>();
		obj1.put("id", "11");
		obj1.put("text", "text11");
		jsonList.add(obj1);

		Map<String, String> obj2 = new HashMap<String, String>();
		obj2.put("id", "12");
		obj2.put("text", "text12");
		jsonList.add(obj2);

		Map<String, String> obj3 = new HashMap<String, String>();
		obj3.put("id", "21");
		obj3.put("text", "text21");
		jsonList.add(obj3);

		Map<String, String> obj4 = new HashMap<String, String>();
		obj4.put("id", "31");
		obj4.put("text", "text31");
		jsonList.add(obj4);

		//孙子列表
		Map<String, String> obj11 = new HashMap<String, String>();
		obj11.put("id", "111");
		obj11.put("text", "text111");
		jsonList2.add(obj11);

		Map<String, String> obj12 = new HashMap<String, String>();
		obj12.put("id", "112");
		obj12.put("text", "text112");
		jsonList2.add(obj12);

	}

	public TextStreamResponse onQuerySub() {
		final String parentId = request.getParameter("parent");
		if(StringUtil.isNull(parentId)){
			return retStream("[]");
		}

		List<Map<String, String>> selList = (List<Map<String, String>>) CollectionUtils
				.select(jsonList, new Predicate() {
					@Override
					public boolean evaluate(Object arg0) {
						Map<String, String> temp = (Map<String, String>) arg0;
						return temp.get("id").startsWith(parentId);
					}
				});
		String ret = JSONUtil.getJsonForListAlias(selList);
		return retStream(ret);
	}

	public TextStreamResponse onQuerySubSub() {
		final String parentId = request.getParameter("parent");
		if(StringUtil.isNull(parentId)){
			return retStream("[]");
		}

		List<Map<String, String>> selList = (List<Map<String, String>>) CollectionUtils
				.select(jsonList2, new Predicate() {
					@Override
					public boolean evaluate(Object arg0) {
						Map<String, String> temp = (Map<String, String>) arg0;
						return temp.get("id").startsWith(parentId);
					}
				});
		String ret = JSONUtil.getJsonForListAlias(selList);
		return retStream(ret);
	}
}
